package cn.org.intelli.zjgflink.controller;

import cn.org.intelli.zjgflink.domain.BackBean;
import cn.org.intelli.zjgflink.service.HdfsOpService;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

@RestController
@RequestMapping({"/jar"})
public class JarFileController {
    private static final Logger log = LoggerFactory.getLogger(cn.org.intelli.zjgflink.controller.JarFileController.class);

    @Autowired
    private HdfsOpService hdfsOpService;

    @Value("${flink.hdfs.jarDir}")
    private String jarDirPath;

    @Value("${flink.hdfs.uploadTmpPath}")
    private String hdfsTmpPath;

    @GetMapping({"/getUploadedFileJars"})
    public BackBean<List> getUploadedFileJars() {
        try {
            List<String> fileNames = this.hdfsOpService.getFileNamesInFolder(this.jarDirPath);
            return BackBean.<List>builder().status(true).data(fileNames).build();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("@GetUploadedFileJars: error is {}", e.getMessage());
            return BackBean.<List>builder().status(false).message(e.getMessage()).build();
        }
    }

    @PostMapping({"/uploadJarFile"})
    public BackBean<String> uploadJarFile(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty())
                throw new Exception("");
            String tmpPath = this.hdfsTmpPath + File.separator + file.getOriginalFilename();
            file.transferTo(new File(tmpPath));
            String hdfsPath = this.jarDirPath + "/" + file.getOriginalFilename();
            this.hdfsOpService.uploadFile2Hdfs(tmpPath, hdfsPath);
            Files.delete(Paths.get(tmpPath));
            return BackBean.<String>builder().status(true).data("success").build();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("@UploadJarFile: error is {}", e.getMessage());
            return BackBean.<String>builder().status(false).message(e.getMessage()).build();
        }
    }

    @PostMapping({"/deleteJarFile"})
    public BackBean<String> deleteJarFile(@RequestBody JSONObject param) {
        try {
            String fileName = param.getString("fileName");
            String hdfsPath = this.jarDirPath + "/" + fileName;
            this.hdfsOpService.deleteFileOrDirectory(hdfsPath);
            return BackBean.<String>builder().status(true).data("success").build();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("@DeleteJarFile: error is {}", e.getMessage());
            return BackBean.<String>builder().status(false).message(e.getMessage()).build();
        }
    }
}
